package com.apexsoft.monitor.front.console.host.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.host.service.HostService;
import com.apexsoft.monitor.front.console.pageModel.HostPageModel;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 主机管理
 */
@Controller("hostController")
@RequestMapping("/console/host")
public class HostController extends BaseFrontController {
    @Autowired
    @Qualifier("hostService")
    private HostService hostService = null;

    /**
     * 分页查询
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listByPage")
    public String listByPage(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        return "console/host/hostList";
    }

    /**
     * 主机监控
     */
    @RequestMapping(value = "/monitor")
    public String monitor(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        return "console/host/hostMonitor";
    }

    @RequestMapping(value = "/loadHostByPage")
    public String getHostList(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("form") HostPageModel hostPageModel)throws Exception {
        Pagination page =new Pagination(hostPageModel.getPageNum(),hostPageModel.getPageSize());
        JSONObject result = this.hostService.listByPage(page,hostPageModel.getHostName());
        this.printJSON(response,result);
        return null;
    }
    @RequestMapping(value = "/getAllHost")
    public String getAllHost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONArray array = new JSONArray();
        JSONObject result = hostService.getAllHost();
        if (result != null && result.getIntValue(Result.CODE) > 0) {
            JSONObject nullObj = new JSONObject();
            nullObj.put("hostCode","");
            nullObj.put("hostName","----请选择----");
            array.add(nullObj);
            array.addAll(1,result.getJSONArray(Result.ROWS));
        }
        this.printJSON(response, array);
        return null;

    }

    @CrudMethod
    @AopAnnotation(description="添加主机")
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = hostService.add(request);
        this.printJSON(response,result);
        return null;
    }
    @CrudMethod
    @AopAnnotation(description="修改主机")
    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = hostService.update(request);
        this.printJSON(response,result);
        return null;
    }
    @CrudMethod
    @AopAnnotation(description="删除主机")
    @RequestMapping(value = "/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        JSONObject result = hostService.delete(id);
        this.printJSON(response,result);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description="主机连接测试")
    @RequestMapping(value = "/hostConnectTest")
    public String hostConnectTest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = hostService.hostConnectTest(request);
        this.printJSON(response,result);
        return null;
    }
}

